package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

type UserInfo struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}
type ArticleInfo struct {
	Title   string `json:"title"`
	Content int    `json:"content"`
}
type Respponse struct {
	Code int    `json:"code"`
	Data any    `json:"data"`
	Msg  string `json:"msg"`
}

func UserListView(c *gin.Context) {
	var userList []UserInfo = []UserInfo{
		{"a", 1},
		{"b", 2},
		{"c", 3},
	}
	c.JSON(200, Respponse{0, userList, "ok"})
}
func ArticleListView(c *gin.Context) {
	var userList []ArticleInfo = []ArticleInfo{
		{"go", 1},
		{"java", 2},
		{"c", 3},
	}
	c.JSON(200, Respponse{0, userList, "ok"})
}
func getONe(c *gin.Context) {

	c.JSON(200, Respponse{0, UserInfo{"a", 1}, "ok"})
}
func userHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("userHandler...in")
		c.Next()
		fmt.Println("userHandler...out")
	}
}
func articleHandler(c *gin.Context) {
	fmt.Println("articleHandler...in")
	c.Next()
	fmt.Println("articleHandler...out")
}
func articleGroupInit(apiGroup *gin.RouterGroup) {
	articleManage := apiGroup.Group("articleManage")
	{
		articleManage.Use(articleHandler)
		articleManage.GET("/articles", ArticleListView)
	}
}
func main() {
	r := gin.Default()
	apiGroup := r.Group("/api")
	userManage := apiGroup.Group("userManage").Use(userHandler())
	{
		userManage.GET("/users", UserListView)
	}
	articleGroupInit(apiGroup)

	r.Run(":8080")
}
